<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no" />
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .main {
        	padding: 10px 15px;
        }
        .main ul li {
        	margin-bottom: 10px;
        }
        .main ul li:last-child {
        	margin-bottom: 0;
        }
        .main ul li .li_box {
        	height: 68px;
			border-radius: 10px;
			display: -webkit-box;
			display: -webkit-flex;
			position: relative
        }
        .main ul li .li_box .li_boxl {
        	width: 70px;
        	height: 68px;
        	line-height: 68px;
			background-color: #cdcdcd;
        	font-size: 24px;
        	color: #fff;
        	text-align: center;
        	border-top-left-radius: 10px;
        	border-bottom-left-radius: 10px;
        }
        .main ul li .li_box.active .li_boxl {
        	background-color: #e62442;
        }
        .main ul li .li_box .li_boxl span {
        	font-size: 15px;
        	vertical-align: top;
        	margin-top: -3px;
        }
        .main ul li .li_box .li_boxc {
        	width: 0;
        	height: 68px;
        	background-color: #fff;
        	padding-left: 14px;
        	padding-right: 5px;
        	flex: 1;
        	-webkit-flex: 1;
        	box-flex: 1;
        	-webkit-box-flex: 1;
        	display: -webkit-box;
        	-webkit-box-align: center;
        }
        .main ul li .li_box .li_boxc .title {
        	font-size: 16px;
        	color: #333;
        	margin-bottom: 8px;
        }
        .main ul li .li_box .li_boxc .time {
        	font-size: 12px;
        	color: #999;
        }
        .main ul li .li_box .li_boxr {
        	height: 68px;
        	padding-right: 10px;
        	background-color: #fff;
        	display: -webkit-box;
        	-webkit-box-align: center;
        	border-top-right-radius: 10px;
        	border-bottom-right-radius: 10px;
        }
        .main ul li .li_box .li_boxr .btn {
        	width: 50px;
        	height: 20px;
        	line-height: 20px;
        	text-align: center;
        	border-radius: 5px;
        	background-color: #cdcdcd;
			color: #fff;
			font-size: 12px;
        }
        .main ul li .li_box.active .li_boxr .btn {
			background-color: #e62442;
        }
        .circle_box .circle1 {
        	width: 12px;
        	height: 12px;
        	border-radius: 50%;
        	background-color: #f8f8f8;
        	position: absolute;
        	left: 64px;
        	top: -6px;
        	z-index: 99;
        }
        .circle_box .circle2 {
        	width: 5px;
        	height: 5px;
        	border-radius: 50%;
        	background-color: #f8f8f8;
        	position: absolute;
        	left: 68px;
        	top: 16px;
        	z-index: 99;
        }
        .circle_box .circle3 {
        	width: 5px;
        	height: 5px;
        	border-radius: 50%;
        	background-color: #f8f8f8;
        	position: absolute;
        	left: 68px;
        	top: 0;
        	bottom: 0;
        	margin: auto;
        	z-index: 99;
        }
        .circle_box .circle4 {
        	width: 5px;
        	height: 5px;
        	border-radius: 50%;
        	background-color: #f8f8f8;
        	position: absolute;
        	left: 68px;
        	bottom: 16px;
        	z-index: 99;
        }
        .circle_box .circle5 {
        	width: 12px;
        	height: 12px;
        	border-radius: 50%;
        	background-color: #f8f8f8;
        	position: absolute;
        	left: 64px;
        	bottom: -6px;
        	z-index: 99;
        }
        .without_data{
            display: none;
            color:#ccc;
            text-align:center;
            padding-top: 15px;
        }
    </style>
</head>
<body>
    <div class="main">
        <p class="without_data">暂无优惠</p>
    	<ul id="data_ul">
<!--     		<li>
    			<div class="li_box active">
    				<div class="li_boxl"><span>$</span>3</div>
    				<div class="li_boxc">
    					<div>
    						<div class="title">仅限本店使用</div>
    						<div class="time">使用时间：2017.09.14-2017.09.16</div>
    					</div>
    				</div>
    				<div class="li_boxr">
    					<div class="btn">领取</div>
    				</div>
    				<div class="circle_box">
    					<div class="circle1"></div>
    					<div class="circle2"></div>
    					<div class="circle3"></div>
    					<div class="circle4"></div>
    					<div class="circle5"></div>
    				</div>
    			</div>
    		</li>
    		<li>
    			<div class="li_box">
    				<div class="li_boxl"><span>$</span>3</div>
    				<div class="li_boxc">
    					<div>
    						<div class="title">仅限本店使用</div>
    						<div class="time">使用时间：2017.09.14-2017.09.16</div>
    					</div>
    				</div>
    				<div class="li_boxr">
    					<div class="btn">已领取</div>
    				</div>
    				<div class="circle_box">
    					<div class="circle1"></div>
    					<div class="circle2"></div>
    					<div class="circle3"></div>
    					<div class="circle4"></div>
    					<div class="circle5"></div>
    				</div>
    			</div>
    		</li> -->
    	</ul>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript" src="../script/doT.min.js"></script>
<script id="dataTpl" type="text/x-dot-template">
    {{ for(var i=0;i<it.length;i++){ }}
        <li>
            <div class="li_box active">
                <div class="li_boxl"><span>$</span>{{=it[i]['price']}}</div>
                <div class="li_boxc">
                    <div>
                        <div class="title">仅限本店使用</div>
                        <div class="time">使用时间：{{=it[i]['begin_time']}}-{{=it[i]['end_time']}}</div>
                    </div>
                </div>
                <div class="li_boxr">
                    <div class="btn" id="ticket_{{=it[i]['id']}}" onclick="fnUseTicket({{=it[i]['id']}},{{=it[i]['price']}});" tapmode>使用</div>
                </div>
                <div class="circle_box">
                    <div class="circle1"></div>
                    <div class="circle2"></div>
                    <div class="circle3"></div>
                    <div class="circle4"></div>
                    <div class="circle5"></div>
                </div>
            </div>
        </li>
    {{ }; }}
</script>
<script type="text/javascript">
    apiready = function(){
        shop_id = api.pageParam.shop_id;
        must_pay = api.pageParam.must_pay;
        old_ticket_id = api.pageParam.old_ticket_id;

        fnShopTicket();

        api.addEventListener({
            name: 'loginEvent'
        }, function(ret, err) {
            if (ret) {
                fnShopTicket(1);
            }
        });
    };

    function fnShopTicket(type) {
        user = $api.getStorage('user_data');
        uid = user?user.id:'';
        api.ajax({
            url: rootPath+'/api/user/shop_ticket',
            method: 'get',
            data: {
                values: { 
                    shop_id: shop_id,
                    uid : user.id,
                    token:user.token,
                    price : must_pay
                },

            }
        }, function(ret, err) {
            if(type == 1) {
                $('#data_ul').empty();
            }
            if (ret) {
                if(ret.status == 200 ) {
                    appendDotData(ret.data,'dataTpl','data_ul',true);
                    if(old_ticket_id){
                        $("#ticket_"+old_ticket_id).parents('.li_box').removeClass('active');
                        $("#ticket_"+old_ticket_id).html('已选中');
                    }
                }else {
                    $('.without_data').show();
                }
            }
        });
    }

    function fnUseTicket(ticket_id,price) {
        api.sendEvent({
            name: 'useTicketEvent',
            extra: {
                ticket_id: ticket_id, 
                price: price
            }
        });
        api.closeWin();
    }
</script>
</html>